//数字滚动
// 结构说明
// 1.代码前面的分号,可以防止多个文件压缩合并比为其他文件的最后一行语句没加分号,而引起合并后的语法错误

// 2.匿名函数(function(){})(); :由于JavaScript执行表达式是从圆括号里面到外面,所以可以用圆括号强制执行声明的函数.避免函数体内和外部的变量冲突.

// 3.$实参:$是jQuery的简写,这里$接受jQuery对象,也是为了避免$变量冲突,保证插件可以正常运行.

// 4.window,document实参分别接受window,document对象,而且window,document对象全都是全局环境下的,而在函数体内window,document其实是局部变量,不是全局的window,document对象.好处:提高性能,减少作用域链的查询时间,若在函数体内需要多次调用window或document对象,这样把window或document对象当做参数传进去,这样做是很必要的.拖插件用不到这2个对象,那就不用传递这两个参数.

// 5.undefined形参:

// undefined在老一辈的浏览器是不被支持的，直接使用会报错，js框架要考虑到兼容性，因此增加一个形参undefined
(function($, window, document) {
    "use strict";//严格模式
   //默认设置对象
    var defaults = {
        deVal: 0,       //传入值
        className:'dataNums',   //样式名称
        digit:''    //默认显示几位数字
    };


    function rollNumDaq(obj, options){
        this.obj = obj; //把实参的obj传给this的obj属性
        this.options = $.extend(defaults, options);
        this.init = function(){
             this.initHtml(obj,defaults);
        }
    }
    rollNumDaq.prototype = {
        initHtml: function(obj,options){
            var strHtml = '<ul class="' + options.className + ' inrow">';
            var valLen = options.digit ||  (options.deVal + '').length;
            if(obj.find('.'+options.className).length <= 0){
                for(var i = 0; i<  valLen; i++){
                    strHtml += '<li class="dataOne "><div class="dataBoc"><div class="tt" t="38"><span class="num0">0</span> <span class="num1">1</span> <span class="num2">2</span> <span class="num3">3</span> <span class="num4">4</span><span class="num5">5</span> <span class="num6">6</span> <span class="num7">7</span> <span class="num8">8</span> <span class="num9">9</span><span class="num0">0</span> <span class="num1">1</span> <span class="num2">2</span> <span class="num3">3</span> <span class="num4">4</span><span class="num5">5</span> <span class="num6">6</span> <span class="num7">7</span> <span class="num8">8</span> <span class="num9">9</span></div></div></li>';
                }
                strHtml += '</ul>';
                obj.html(strHtml);
            }
            this.scroNum(options);
        },
        scroNum: function(options){
            //倒计时的数字
            var number = options.deVal; 
            
            var $num_item = $('.' + options.className).find('.tt');
            var h = $('.dataBoc').height();
            $num_item.css('transition','all 2s ease-in-out');
            var numberStr = number.toString();
            if(numberStr.length <= $num_item.length - 1){
                var tempStr = '';
                for(var a = 0; a < $num_item.length - numberStr.length; a++){
                    tempStr += '0';
                }
                numberStr = tempStr + numberStr;
            }

            var numberArr = numberStr.split('');
            $num_item.each(function(i, item) {
                setTimeout(function(){
                    $num_item.eq(i).css('top',-parseInt(numberArr[i])*h - h*10 + 'px');
                },i*100)
            });
        }
    }


    $.fn.rollNumDaq = function(options){
        var $that = this;
        var rollNumObj = new rollNumDaq($that, options);
        rollNumObj.init();
    };
})(jQuery, window, document);